home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 July: Mac OS SDK / Dev.CD Jul 97 SDK1.toast / Development Kits (Disc 1) / Open Transport / Sample Code / DTS Sample Code / ShowOTEnetAddr / ShowOTEnetAddr.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-19  |  4.9 KB  |  182 lines  |  [TEXT/CWIE]

  1. /*
  2.     File:        ShowOTEnetAddr.c
  3.  
  4.     Contains:    Simple app to print out Ethernet addresses for any Ethernet card on
  5.                 any Mac which has Open Transport installed.  
  6.                 This includes the PCI based Macs.
  7.  
  8.  
  9.     Copyright:    © 1993-1995 by Apple Computer, Inc., all rights reserved.
  10.  
  11.     Change History (most recent first):
  12.     2/6/96    rkubota    (1)    initialized the index variable to 0 in main
  13.  
  14.     To Do:
  15. */
  16.  
  17.  
  18. #include <stdio.h>
  19. #include <Types.h>
  20. #include <Memory.h>
  21. #include <Resources.h>
  22. #include <Events.h>
  23.  
  24. #include <OpenTransport.h>            // open transport files            
  25. #include <OpenTptLinks.h>
  26.  
  27. struct Address8022
  28. {
  29.     OTAddressType    fAddrFamily;
  30.     UInt8            fHWAddr[k48BitAddrLength];
  31.     UInt16            fSAP;
  32.     UInt8            fSNAP[k8022SNAPLength];
  33. };
  34. typedef struct Address8022 Address8022;
  35.  
  36. #define kTrue    1
  37. #define kFalse    0
  38.  
  39. //-----------------------------------------------------------------------------------------
  40. // Globals
  41. //-----------------------------------------------------------------------------------------
  42.  
  43. EndpointRef        gEndpoint1;
  44.  
  45. //-----------------------------------------------------------------------------------------
  46. // prototypes
  47. //-----------------------------------------------------------------------------------------
  48. OSStatus DoBind(void);
  49. OSStatus GetEthernetAddress(char *slotName);
  50.  
  51. /*******************************************************************************
  52. ** DoBindENET
  53.     For this sample, we need to bind to any ethernet endpoint.  In this case
  54.     we bind to enet protocol type 0x8888.  We use an Address8022 variable
  55.     type for expediency
  56. ********************************************************************************/
  57.  
  58. OSStatus DoBind()
  59. {
  60.     OSStatus        osStatus;
  61.     TBind            gRequestInfo;
  62.     Address8022     theAddr =     {AF_8022, {0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 0x8888,
  63.                                     {0x00,0x00,0x00,0x00,0x00}};
  64.     
  65.         // finish bind information
  66.     gRequestInfo.addr.buf = (UInt8 *)&theAddr;
  67.     gRequestInfo.addr.len = 10;    // family type + Ethernet + type field
  68.                     // don't use sizeof(theAddr) since we are binding to type 1 Ethernet
  69.                     // address, not to an 802.2 address.
  70.     gRequestInfo.addr.maxlen = 0;            
  71.     gRequestInfo.qlen = 0;
  72.     
  73.     if (osStatus = OTBind(gEndpoint1, &gRequestInfo, NULL))
  74.     {
  75.         printf("\nCould not bind an endpoint, error = %d\n", osStatus);
  76.         CloseOpenTransport();
  77.         return    -4;
  78.     }
  79.     
  80.     return osStatus;
  81. }
  82.  
  83. /*******************************************************************************
  84. ** GetEthernetAddress 
  85. ********************************************************************************/
  86.  
  87. OSStatus GetEthernetAddress(char *slotName)
  88. {
  89.     OSStatus         osStatus;
  90.     TBind            returnInfo;
  91.     Address8022     theReturnAddr =     {AF_8022, {0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 0x0000,
  92.                                         {0x00,0x00,0x00,0x00,0x00}};
  93.     
  94.     returnInfo.addr.buf = (UInt8 *)&theReturnAddr;
  95.     returnInfo.addr.maxlen = 10;            // family type + 6 bytes for Ethernet + type
  96.     returnInfo.qlen = 0;
  97.     
  98.     
  99.     osStatus = OTGetProtAddress(gEndpoint1,&returnInfo,NULL);
  100.     
  101.     if (osStatus == kOTNoError)
  102.     {
  103.         if (slotName[0] != 0x00)
  104.             printf("\nAddress for PCI Slot %s => ", slotName);
  105.         else
  106.             printf("\nAddress for Ethernet Built-In => ");
  107.         
  108.         printf("%02x.",(int )theReturnAddr.fHWAddr[0]);
  109.         printf("%02x.",(int )theReturnAddr.fHWAddr[1]);
  110.         printf("%02x.",(int )theReturnAddr.fHWAddr[2]);
  111.         printf("%02x.",(int )theReturnAddr.fHWAddr[3]);
  112.         printf("%02x.",(int )theReturnAddr.fHWAddr[4]);
  113.         printf("%02x",(int )theReturnAddr.fHWAddr[5]);
  114.     }
  115.     else
  116.         printf("\n\nCould not get the Ethernet address, error = %d\n\n", osStatus);
  117.     
  118.     return osStatus;
  119. }
  120.  
  121. /*******************************************************************************
  122. ** main
  123. ********************************************************************************/
  124.  
  125. main()
  126. {
  127.     OSStatus        osStatus;
  128.     OTPortRecord    devicePortRecord;
  129.     Boolean            foundAPort;
  130.     UInt32            index;
  131.     
  132.     printf("\n\nEthernet Address Display v1.1\n");
  133.     printf("\nThis application displays the Ethernet address for the\n");
  134.     printf("Ethernet Built-In port and PCI Ethernet cards.\n\n");
  135.     
  136.     if (osStatus = InitOpenTransport())
  137.     {
  138.         printf("\n\nOpen Transport is not installed!\n\n");
  139.         return 0;
  140.     }
  141.     
  142.     index = 0;
  143.         // iterate thru each OT port record for ethernet ports.    
  144.     while (foundAPort = OTGetIndexedPort(&devicePortRecord,index))
  145.     {
  146.         if ((devicePortRecord.fCapabilities & kOTPortIsDLPI) &&
  147.             (devicePortRecord.fCapabilities & kOTPortIsTPI) &&
  148.             (kOTEthernetDevice == OTGetDeviceTypeFromPortRef(devicePortRecord.fRef)))
  149.         {
  150.     
  151.             gEndpoint1 = OTOpenEndpoint(OTCreateConfiguration(devicePortRecord.fPortName), (OTOpenFlags)NULL, NULL,&osStatus);
  152.     
  153.             if (osStatus == kOTNoError)
  154.             {
  155.                     // we have to bind the endpoint before we can get it's address info
  156.                 osStatus = DoBind();
  157.                 if (osStatus == kOTNoError)
  158.                 {
  159.                     GetEthernetAddress(devicePortRecord.fSlotID);
  160.                     OTUnbind(gEndpoint1);
  161.                 }
  162.                 
  163.                 OTCloseProvider(gEndpoint1);
  164.             }    
  165.                 
  166.                 
  167.         }
  168.         index++;
  169.     }    
  170.     
  171.     
  172.     
  173.         // closing down
  174.     CloseOpenTransport();
  175.     
  176.     printf("\n\nTo quit the application press Command-Q.\n");
  177.     
  178.     return 0;
  179. }
  180.  
  181.  
  182.